package com.opmsys.controller;

import com.opmsys.pojo.dto.AccidentPageDTO;
import com.opmsys.pojo.entity.Accident;
import com.opmsys.result.PageResult;
import com.opmsys.result.Result;
import com.opmsys.service.AccidentService;
import com.opmsys.service.RepairService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Objects;

/**
 * 描述
 *
 * @datatime:2024-06-17 17:50
 */
@RestController
@RequestMapping("/accident")
@Slf4j
public class AccidentController {

    @Autowired
    AccidentService accidentService;

    @Autowired
    RepairService repairService;

    /**
     * 新增事故记录
     *
     * @param accident
     * @return
     */
    @PostMapping
    public Result add(Accident accident){
        log.info("新增事故记录");
        if(accident.getAccidentDate() == null || accident.getPersonNumber() == null || accident.getPumpId() == null || accident.getAccidentDate().equals("")){
            return Result.error("事故日期、负责人、抽油机id均不能为空");
        }
        accidentService.add(accident);
        return Result.success();
    }

    /**
     * 更新事故记录
     *
     * @param accident
     * @return
     */
    @PutMapping
    public Result update(Accident accident){
        if(accident.getId() == null){
            return Result.error("缺失id，错误请求");
        }
        log.info("更新事故记录");
        accidentService.update(accident);
        return Result.success();
    }

    /**
     * 批量删除事故
     *
     * @param ids
     * @return
     */
    @DeleteMapping
    public Result delete(@RequestParam(required = false) List<Integer> ids){
        log.info("删除记录id："+ ids.toString());
        accidentService.delete(ids);
        repairService.deleteByAccidentIds(ids);
        return Result.success();
    }


    /**
     * 按条件事故分页查询
     * @param accidentPageDTO
     * @return
     */
    @GetMapping("/page")
    public Result<PageResult> page(AccidentPageDTO accidentPageDTO){
        log.info("事故分页查询执行,{}",accidentPageDTO);
        PageResult pageResult = accidentService.pageQuery(accidentPageDTO);
        return Result.success(pageResult);
    }


}
